local skel = fk.CreateSkill {
  name = "rmt__ruyuan",
}

Fk:loadTranslationTable{
  ["rmt__ruyuan"] = "如渊",
  [":rmt__ruyuan"] = "一名角色的结束阶段，若其体力值或手牌数为场上唯一值，你可对其造成一点火焰伤害；若两项均为唯一值或其为你，你可先视为使用或重铸一张【铁索连环】。",

  ["#rmt__ruyuan1-invoke"] = "如渊：你可以对 %dest 造成1点火焰伤害",
  ["#rmt__ruyuan2-invoke"] = "如渊：你可以对 %dest 造成1点火焰伤害，且此前可视为使用【铁索】",
  ["rmt__ruyuan_active"] = "如渊",
  ["#rmt__ruyuan-ironchain"] = "如渊：你可视为使用或重铸【铁索连环】",

  ["$rmt__ruyuan1"] = "此心战战，如临深而履薄。",
  ["$rmt__ruyuan2"] = "上下无常，涉道浅、没足深。",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and target.phase == Player.Finish and player:getMark("rmt__ruyuan-round") == 0 and
      not target.dead then
      local n = 0
      if table.every(player.room:getOtherPlayers(target), function (p)
        return p:getHandcardNum() ~= target:getHandcardNum()
      end) then
        n = n + 1
      end
      if table.every(player.room:getOtherPlayers(target), function (p)
        return p.hp ~= target.hp
      end) then
        n = n + 1
      end
      if n > 0 then
        if player == target then n = 2 end
        event:setCostData(self, { tos = {target}, n = n })
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local n = event:getCostData(self).n
    return player.room:askToSkillInvoke(player, {
      skill_name = skel.name, prompt = "#rmt__ruyuan".. n .."-invoke::"..target.id
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event:getCostData(self).n == 2 then
      local _,dat = room:askToUseActiveSkill(player, { skill_name = "rmt__ruyuan_active", prompt = "#rmt__ruyuan-ironchain" })
      if dat then
        if #dat.targets > 0 then
          room:useVirtualCard("iron_chain", nil, player, dat.targets, skel.name)
        else
          room:broadcastPlaySound("./audio/system/recast")
          player:drawCards(1, "recast")
        end
      end
    end
    room:damage{
      from = player,
      to = target,
      damage = 1,
      damageType = fk.FireDamage,
      skillName = skel.name,
    }
  end,
})

local skel2 = fk.CreateSkill {
  name = "rmt__ruyuan_active",
}

skel2:addEffect("active", {
  card_filter = Util.FalseFunc,
  interaction = function()
    return UI.ComboBox { choices = {"method_use", "recast"} }
  end,
  target_filter = function (self, player, to, selected, selected_cards)
    if self.interaction.data == "recast" then return false end
    local card = Fk:cloneCard("iron_chain")
    card.skillName = skel.name
    return #selected < card.skill:getMaxTargetNum(player, card) and player:canUseTo(card, to)
  end,
  feasible = function (self, player, selected, selected_cards, card)
    return #selected > 0 or self.interaction.data == "recast"
  end,
})


return {skel, skel2}
